//
//  MainViewController.swift
//  Weibo
//
//  Created by 唐润欣 on 16/6/17.
//  Copyright © 2016年 唐润欣. All rights reserved.
//

import UIKit

//swift文件中，对于不需要公开的属性和方法，我们需要加一个private
//面试题： 在开发中你是怎么调试bug的？
//1. 用断点调式
//2. 看调用堆栈
//3. 暴力测试， 用NSLog和注释部分代码的方法调试bug
//4. 直接google或百度

//去墙： shadowsock

//tabBar的子view是在viewWillAppear才创建，定制tabBar时，需要将定制的子view移到最上层： bringSubviewToFront

// MARK: - class里面只放生命周期方法，构造方法，属性
class MainViewController: UITabBarController {
    
    var isNewFeature: Bool {
        
        return NSBundle.checkIfNewVersion()
    }
    
    //暴力测试，排除法
    //action如果有参数，需要在方法名后面加冒号。一个参数加一个，两个参数加两个
    private lazy var composeButton: UIButton = UIButton(title: "", image: "tabbar_compose_icon_add", backGroundImage: "tabbar_compose_button", target: self, action: "compose", event: .TouchUpInside)
    
    override func viewDidLoad() {
        
        super.viewDidLoad()
        view.backgroundColor = UIColor.whiteColor()
        
        //添加子控制器
        addChildViewControllers()
        addComposeButton()
        setTabBar()
        
        //加载欢迎页或新功能页
        showNewFeatureOrWelcomeView()
    }
    
    override func viewWillAppear(animated: Bool) {
        
        super.viewWillAppear(animated)
        
        tabBar.bringSubviewToFront(composeButton)
    }
}

// MARK: - 分类可以相将同逻辑的代码放到一个分类里
extension MainViewController {
    
    /**
     添加子控制器
     
     - parameter controller 子控制器
     - parameter title title
     - parameter iconImageName tabBarItem的图片名字
     */
    private func addChildViewController(childController: UIViewController, title: String, iconImageName: String?) {
        
        let navigationHome = UINavigationController(rootViewController: childController)
        childController.title = title
        
        //定制tabbar文字和颜色
//        childController.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.grayColor()], forState: .Normal)
//        childController.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.orangeColor()], forState: .Selected)
        
        if let iconImageName = iconImageName {
            
            let selectedImageName = "\(iconImageName)_selected"
            
//            childController.tabBarItem.image = UIImage(named: iconImageName)?.imageWithRenderingMode(.AlwaysOriginal)
//            childController.tabBarItem.selectedImage = UIImage(named: selectedImageName)?.imageWithRenderingMode(.AlwaysOriginal)
            
            childController.tabBarItem.image = UIImage(named: iconImageName)
            childController.tabBarItem.selectedImage = UIImage(named: selectedImageName)
        }
        
        addChildViewController(navigationHome)
    }
    
    /**
     添加所有的子控制器
     */
    private func addChildViewControllers() {
        
        let home = HomeViewController()
        addChildViewController(home, title: "首页", iconImageName: "tabbar_home")
        
        let message = MessageViewController()
        addChildViewController(message, title: "消息", iconImageName: "tabbar_message_center")
        
        //占位控制器
        let viewController = UIViewController()
        addChildViewController(viewController, title: "", iconImageName: nil)
        
        let discover = DiscoverViewController()
        addChildViewController(discover, title: "发现", iconImageName: "tabbar_discover")
        
        let profile = ProfileViewController()
        addChildViewController(profile, title: "我", iconImageName: "tabbar_profile")
    }
    
    private func addComposeButton() {
        
        self.tabBar.addSubview(composeButton)
        
        //根据一个frame，向内或向外生成一个新的frame
        let tabBarItemWidth = tabBar.bounds.size.width / 5
        let composeFrame = CGRectInset(tabBar.bounds, tabBarItemWidth * 2, 0)
        composeButton.frame = composeFrame
    }
    
    //设置tabbar
    private func setTabBar() {
        
        //设置tabbar的阴影和背影颜色
        tabBar.backgroundImage = UIImage(named: "tabbar_background")
        tabBar.shadowImage = UIImage.createAdotImage(UIColor(white: 0.9, alpha: 1.0))
    }
    
    //unrecognized selector sent to instance 0x7ff58a804bb0':找不到指定的方法
    //selecotor是在运行时动态地向MainViewControll发送 compse 的消息。
    //@objc可以向MainViewControll发送 compse 的消息
    //用selector调用一个方法时，使用。
    @objc private func compose(button: UIButton) {
        
        print("发布")
    }
}

extension MainViewController {
    
    func showNewFeatureOrWelcomeView() {
        
        //1. 判断用户是否登录
        if UserAccount.sharedUserAccount.isLogin {
            
            //2. 判断是否有新特性
            if isNewFeature {
                
                //3. 展示欢迎页/新特性页
                let newFeatureView = NewFeatureView()
                view.addSubview(newFeatureView)
                
            } else {
                
                let welcomeView = WelcomeView()
                view.addSubview(welcomeView)
            }
        }
    }
}
